import java.rmi.*;
import java.net.*;
import java.rmi.server.*;

public class RMIServerImpl extends UnicastRemoteObject implements RMIServer {
	public RMIServerImpl() throws RemoteException {
		try 
		{
			Naming.rebind("rmi:///Server", this);
		} 
		catch (MalformedURLException e) 
		{
			System.out.println("URL ungültig:\n" + e.getMessage());
		}
	}

	public void setString(String str) {
		// Ausgabe des vom Client gesendeten Strings
		System.out.println("Übergeben von Client: " + str);
		
		// Callback an Client
		System.out.println("Aufruf von Client Funktion");
		try 
		{
			RMIClient client = (RMIClient) Naming.lookup("rmi://localhost/Client");
			client.setString("Hallo Zurueck");
		} 
		catch (MalformedURLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) 
	{
		try 
		{
			RMIServerImpl server = new RMIServerImpl();
		} 
		
		catch (RemoteException e) 
		{
			System.out.println("Fehler beim Instanziieren");
		}
	}
	
	public void setStudent(Student stud)
	{
		//System.out.println("Aufruf von Client Funktion");
		try 
		{
			RMIClient client = (RMIClient) Naming.lookup("rmi://localhost/Client");
			
			//Student Füllen
			stud.setName("Tobias");
			stud.setNumber(72489);
			
			// Student zurück an Client schicken.
			client.setStudent(stud);
		} 
		catch (MalformedURLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}